package cn.pconline.autoclub.util.ubb.node;

import org.apache.commons.lang.StringUtils;

import cn.pconline.autoclub.util.ubb.EscapeUtils;
import cn.pconline.autoclub.util.ubb.Node;


/**
 *
 * @author xiawen
 */
public class AudioNode extends AbstractNode {

    public AudioNode() {
    }

    private AudioNode(String name, String attribute) {
        super.name = name;
        super.attribute = attribute;
    }

    @Override
    public StringBuilder getText(java.util.LinkedList<Node> stack) {
        StringBuilder sb = new StringBuilder();
        String width = "400";
        String height = "64";
        String needStr = "http://"; // 需要插入的字符串
		if (!StringUtils.startsWithIgnoreCase(text.toString(), needStr)) {
			text.insert(0, needStr); // 如果不包含http:// 就强制性增加。
		}

        if (attribute != null) {
            String[] wh = attribute.split("[x|\\,]");
            if (wh.length == 2) {
                width = wh[0];
                height = wh[1];
            }
        }      
        String str = text.toString().trim();  
        if (str.indexOf(".mp3") > 0 || str.indexOf(".wma") > 0 || str.indexOf(".mid") > 0 || str.indexOf(".wav") > 0) {
            sb.append("<object classid=\"clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6\" width=\"").append(width).append("\" height=\"64\"><param name=\"invokeURLs\" value=\"0\"><param name=\"autostart\" value=\"").append("\" /><param name=\"url\" value=\"").append(text).append("\" /><embed src=\"").append(text).append("\" autostart=\"").append(0).append("\" type=\"application/x-mplayer2\" width=\"").append(width).append("\" height=\"64\"></embed></object>");
        }
        return sb;
    }

    @Override
    public void appendText(CharSequence text) {
        EscapeUtils.appendAttribute(text, super.text);
    }

    @Override
    public Node copyWithoutText() {
        return new AudioNode(name, attribute);
    }

    @Override
    public Node build(String name, String attribute) {
        return new AudioNode(name, attribute);
    }
}
